﻿using System;
using System.Linq;

using SimbaRGVMonitor.Common.Data;

namespace SimbaRGVMonitor.Models
{
    class ScheduleModel
    {
        public ScheduleModel()
        {
            InitializeSettings();
        }

        //====================================================================================================
        //以上是类型构造函数
        //----------------------------------------------------------------------------------------------------
        //以下是类型字段
        //====================================================================================================

        private readonly PathCollection paths = new PathCollection();
        private readonly RobotCollection robots = new RobotCollection();
        private readonly VertexCollection vertexes = new VertexCollection();

        //====================================================================================================
        //以上是类型字段
        //----------------------------------------------------------------------------------------------------
        //以下是类型属性
        //====================================================================================================

        public ReadOnlyDictionary<string, Path> Paths
        {
            get { return paths; }
        }

        public ReadOnlyDictionary<string, Robot> Robots
        {
            get { return robots; }
        }

        public ReadOnlyDictionary<string, Vertex> Vertexes
        {
            get { return vertexes; }
        }

        //====================================================================================================
        //以上是类型属性
        //----------------------------------------------------------------------------------------------------
        //以下是类型事件及触发函数
        //====================================================================================================

        public event EventHandler<PathEventArgs> PathAdded;
        private void OnPathAdded(Path path)
        {
            var e = new PathEventArgs(path);
            PathAdded.TriggerEvent(this, e);
        }

        public event EventHandler<PathEventArgs> PathUpdated;
        private void OnPathUpdated(Path path)
        {
            var e = new PathEventArgs(path);
            PathUpdated.TriggerEvent(this, e);
        }

        public event EventHandler<PathEventArgs> PathRemoved;
        private void OnPathRemoved(Path path)
        {
            var e = new PathEventArgs(path);
            PathRemoved.TriggerEvent(this, e);
        }

        public event EventHandler<RobotEventArgs> RobotAdded;
        private void OnRobotAdded(Robot robot)
        {
            var e = new RobotEventArgs(robot);
            RobotAdded.TriggerEvent(this, e);
        }

        public event EventHandler<RobotEventArgs> RobotUpdated;
        private void OnRobotUpdated(Robot robot)
        {
            var e = new RobotEventArgs(robot);
            RobotUpdated.TriggerEvent(this, e);
        }

        public event EventHandler<VertexEventArgs> VertexAdded;
        private void OnVertexAdded(Vertex vertex)
        {
            var e = new VertexEventArgs(vertex);
            VertexAdded.TriggerEvent(this, e);
        }

        public event EventHandler<VertexEventArgs> VertexUpdated;
        private void OnVertexUpdated(Vertex vertex)
        {
            var e = new VertexEventArgs(vertex);
            VertexUpdated.TriggerEvent(this, e);
        }

        public event EventHandler<VertexEventArgs> VertexRemoved;
        private void OnVertexRemoved(Vertex vertex)
        {
            var e = new VertexEventArgs(vertex);
            VertexRemoved.TriggerEvent(this, e);
        }

        //====================================================================================================
        //以上是类型事件及触发函数
        //----------------------------------------------------------------------------------------------------
        //以下是初始化设置
        //====================================================================================================

        private void InitializeSettings()
        {
            paths.ModelAdded += (sender, e) => OnPathAdded(e.Value);
            paths.ModelUpdated += (sender, e) => OnPathUpdated(e.Value);
            paths.ModelRemoved += (sender, e) => OnPathRemoved(e.Value);

            robots.ModelAdded += (sender, e) => OnRobotAdded(e.Value);
            robots.ModelUpdated += (sender, e) => OnRobotUpdated(e.Value);

            vertexes.ModelAdded += (sender, e) => OnVertexAdded(e.Value);
            vertexes.ModelUpdated += (sender, e) => OnVertexUpdated(e.Value);
            vertexes.ModelRemoved += (sender, e) => OnVertexRemoved(e.Value);
        }

        //====================================================================================================
        //以上是初始化设置
        //----------------------------------------------------------------------------------------------------
        //以下是模型更新
        //====================================================================================================

        public void AddRobot(Robot robot)
        {
            var key = robot.RobotNo;
            robots.Add(key, robot);
        }

        public void UpdateRobot(Robot robot)
        {
            var key = robot.RobotNo;
            robots.Update(key, robot);
        }

        public void UpdatePaths(Path[] paths)
        {
            var newPaths = paths.ToDictionary(item => item.PathNo, item => item);
            this.paths.Update(newPaths);
        }

        public void UpdateVertexes(Vertex[] vertexes)
        {
            var newVertexes = vertexes.ToDictionary(item => item.VertexNo, item => item);
            this.vertexes.Update(newVertexes);
        }
    }
}
